package org.example.annotationt;

/**
 * @author Yyming
 * @version 10.0
 * Created by Yyming on 2020/11/5
 */

import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * 定义限额注解
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface One {
    double value() default 100;
}
public class ModifyAnno {

    @One(value = 15000)
    public void Transfer(double value) {
    }
    public static void main(String[] args) {
        try {
            Method transferMoney = ModifyAnno.class.getDeclaredMethod("Transfer", double.class);
            boolean annotationPresent = transferMoney.isAnnotationPresent(One.class);
            if (annotationPresent) {
                One annotation = transferMoney.getAnnotation(One.class);
                double v = annotation.value();
                System.out.println(v);
                InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
                Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
                memberValues.setAccessible(true);
                Map map = (Map) memberValues.get(invocationHandler);
                map.put("value", 20000.0);
            }
        } catch (NoSuchMethodException | NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
       value();
    }
    public static void value() {
        try {
            Method transferMoney = ModifyAnno.class.getDeclaredMethod("Transfer", double.class);
            boolean annotationPresent = transferMoney.isAnnotationPresent(One.class);
            if (annotationPresent) {
                One annotation = transferMoney.getAnnotation(One.class);
                double v = annotation.value();
                System.out.println(v);
            }
        } catch (NoSuchMethodException  e) {
            e.printStackTrace();
        }
    }
}
